using System;
using UnityEditor;
using UnityEngine;
using System.Collections.Generic;
using System.Reflection;
using EntityKit.Runtime;
using Object = UnityEngine.Object;
using System.Linq;

namespace EntityKit.Editor
{
    [CustomEditor(typeof(Entity), true)]
    [CanEditMultipleObjects]
    public class EntityInspector : UnityEditor.Editor
    {
        #region Fields

        private bool showEntityInfo = true;
        private bool showStateMachine = true;
        private bool showMVC = true;
        private bool showPublicFields = true;
        private bool showEntityComponents = true;
        private SerializedProperty entityIDProperty;
        
        // 样式定义
        private GUIStyle m_BoxStyle;
        private GUIStyle m_HeaderStyle;
        private GUIStyle m_FoldoutStyle;
        private GUIStyle m_SubHeaderStyle;
        private GUIStyle m_LabelStyle;
        private GUIStyle m_BoldLabelStyle;
        private GUIStyle m_ValueLabelStyle;
        private GUIStyle m_SectionHeaderStyle;
        private GUIStyle m_EnumStyle;
        private GUIStyle m_PropertyFieldStyle;

        private const string FOLDOUT_PREFIX = "➤ ";
        private const string FOLDOUT_OPEN_PREFIX = "▼ ";
        private const string FOLDOUT_CLOSED_PREFIX = "▶ ";

        #endregion

        #region Unity Lifecycle

        private void OnEnable()
        {
            entityIDProperty = serializedObject.FindProperty("mEntityID");
        }

        public override void OnInspectorGUI()
        {
            InitStyles();
            serializedObject.Update();

            DrawEntityInfo();
            
            var entity = target as Entity;
            if (entity != null)
            {
                if (IsMvcEntity(entity))
                {
                    DrawSimpleSceneEntityMVC();
                }
                else
                {
                    DrawEntityComponents(entity);
                }
            }
            
            DrawStateMachineInfo();
            DrawPublicFields();
            
            // 绘制 Entity 对象自身的其他 public 变量
            //DrawEntityScriptVariables();

            serializedObject.ApplyModifiedProperties();
        }

        #endregion

        #region Styles

        private void InitStyles()
        {
            if (m_BoxStyle == null)
            {
                m_BoxStyle = new GUIStyle(EditorStyles.helpBox);
                m_BoxStyle.padding = new RectOffset(10, 10, 10, 10);
                m_BoxStyle.margin = new RectOffset(0, 0, 5, 5);
            }

            if (m_HeaderStyle == null)
            {
                m_HeaderStyle = new GUIStyle(EditorStyles.largeLabel);
                m_HeaderStyle.fontStyle = FontStyle.Bold;
                m_HeaderStyle.fontSize = 16;
                m_HeaderStyle.margin = new RectOffset(0, 0, 5, 10);
                m_HeaderStyle.normal.textColor = EditorGUIUtility.isProSkin ? new Color(0.8f, 0.8f, 0.8f) : new Color(0.2f, 0.2f, 0.2f);
            }

            if (m_FoldoutStyle == null)
            {
                m_FoldoutStyle = new GUIStyle(EditorStyles.foldout);
                m_FoldoutStyle.fontStyle = FontStyle.Bold;
                m_FoldoutStyle.fontSize = 14;
                m_FoldoutStyle.margin = new RectOffset(0, 0, 5, 5);
            }

            if (m_SectionHeaderStyle == null)
            {
                m_SectionHeaderStyle = new GUIStyle(EditorStyles.boldLabel);
                m_SectionHeaderStyle.fontSize = 12;
                m_SectionHeaderStyle.margin = new RectOffset(0, 0, 5, 5);
                m_SectionHeaderStyle.normal.textColor = EditorGUIUtility.isProSkin ? new Color(0.7f, 0.85f, 1.0f) : new Color(0.1f, 0.3f, 0.6f);
            }

            if (m_SubHeaderStyle == null)
            {
                m_SubHeaderStyle = new GUIStyle(EditorStyles.boldLabel);
                m_SubHeaderStyle.margin = new RectOffset(0, 0, 3, 3);
                m_SubHeaderStyle.fontSize = 11;
            }

            if (m_LabelStyle == null)
            {
                m_LabelStyle = new GUIStyle(EditorStyles.label);
                m_LabelStyle.margin = new RectOffset(0, 0, 2, 2);
                m_LabelStyle.fontSize = 11;
            }

            if (m_EnumStyle == null)
            {
                m_EnumStyle = new GUIStyle(EditorStyles.popup);
                m_EnumStyle.fontSize = 11;
                m_EnumStyle.margin = new RectOffset(0, 0, 2, 2);
            }

            if (m_BoldLabelStyle == null)
            {
                m_BoldLabelStyle = new GUIStyle(EditorStyles.boldLabel);
                m_BoldLabelStyle.margin = new RectOffset(0, 0, 2, 2);
                m_BoldLabelStyle.fontSize = 11;
            }

            if (m_ValueLabelStyle == null)
            {
                m_ValueLabelStyle = new GUIStyle(EditorStyles.label);
                m_ValueLabelStyle.margin = new RectOffset(0, 0, 2, 2);
                m_ValueLabelStyle.fontSize = 11;
                m_ValueLabelStyle.normal.textColor = EditorGUIUtility.isProSkin ? new Color(0.7f, 0.9f, 0.7f) : new Color(0.1f, 0.5f, 0.1f);
            }
            
            if (m_PropertyFieldStyle == null)
            {
                m_PropertyFieldStyle = new GUIStyle(EditorStyles.textField);
                m_PropertyFieldStyle.fontSize = 11;
                m_PropertyFieldStyle.margin = new RectOffset(0, 0, 2, 2);
                m_PropertyFieldStyle.padding = new RectOffset(4, 4, 2, 2);
            }
        }

        #endregion

        #region Entity Info

        private void DrawEntityInfo()
        {
            GUILayout.BeginVertical(m_BoxStyle);
            
            showEntityInfo = EditorGUILayout.Foldout(showEntityInfo, 
                new GUIContent((showEntityInfo ? FOLDOUT_OPEN_PREFIX : FOLDOUT_CLOSED_PREFIX) + "Entity Information"), 
                true, m_FoldoutStyle);
            
            if (showEntityInfo)
            {
                EditorGUI.indentLevel++;
                EditorGUILayout.PropertyField(entityIDProperty, new GUIContent("Entity ID"));
                EditorGUI.indentLevel--;
            }
            
            GUILayout.EndVertical();
            EditorGUILayout.Space();
        }

        #endregion

        #region State Machine

        private void DrawStateMachineInfo()
        {
            var entity = target as Entity;
            if (entity == null) return;

            if (IsStateMachineEntity(entity))
            {
                GUILayout.BeginVertical(m_BoxStyle);
                
                showStateMachine = EditorGUILayout.Foldout(showStateMachine, 
                    new GUIContent((showStateMachine ? FOLDOUT_OPEN_PREFIX : FOLDOUT_CLOSED_PREFIX) + "State Machine"), 
                    true, m_FoldoutStyle);
                
                if (showStateMachine)
                {
                    EditorGUI.indentLevel++;

                    var stateMachine = GetStateMachine(entity);
                    if (stateMachine != null)
                    {
                        DrawCurrentStateInfo(stateMachine);
                        DrawStateNodes(stateMachine);
                        DrawStateTransitions(stateMachine);
                    }

                    EditorGUI.indentLevel--;
                }
                GUILayout.EndVertical();
                EditorGUILayout.Space();
            }
        }

        private StateMachine GetStateMachine(Entity entity)
        {
            var stateMachineField = typeof(Entity).GetField("mStateMachine",
                BindingFlags.NonPublic | BindingFlags.Instance);
            
            return stateMachineField?.GetValue(entity) as StateMachine;
        }

        private bool IsStateMachineEntity(Entity entity)
        {
            var stateMachineField = typeof(Entity).GetField("mStateMachine",
                BindingFlags.NonPublic | BindingFlags.Instance);
            return stateMachineField != null;
        }

        private void DrawCurrentStateInfo(StateMachine stateMachine)
        {
            // 获取当前状态
            var currentNodeProp = typeof(StateMachine).GetProperty("CurrentNode",
                BindingFlags.Public | BindingFlags.Instance);
            
            if (currentNodeProp != null)
            {
                var currentNode = currentNodeProp.GetValue(stateMachine, null) as IStateNode;
                EditorGUILayout.LabelField("Current State", 
                    currentNode != null ? currentNode.GetType().Name : "None", 
                    m_BoldLabelStyle);
                EditorGUILayout.Space(2);
            }
            
            EditorGUILayout.Space(5);
        }

        private void DrawStateNodes(StateMachine stateMachine)
        {
            EditorGUILayout.LabelField("States", m_SectionHeaderStyle);
            
            // 状态节点数量
            var getAllNodesMethod = typeof(StateMachine).GetMethod("GetAllNodes",
                BindingFlags.Public | BindingFlags.Instance);

            if (getAllNodesMethod != null)
            {
                if (getAllNodesMethod.Invoke(stateMachine, null) is IReadOnlyDictionary<Type, IStateNode> nodesDict)
                {
                    EditorGUILayout.LabelField($"Count: {nodesDict.Count}", m_BoldLabelStyle);
                    EditorGUILayout.Space(2);

                    if (nodesDict.Count > 0)
                    {
                        EditorGUI.indentLevel++;
                        foreach (var kvp in nodesDict)
                        {
                            EditorGUILayout.LabelField(kvp.Value.GetType().Name, m_LabelStyle);
                        }

                        EditorGUI.indentLevel--;
                    }
                }
            }
        }

        private void DrawStateTransitions(StateMachine stateMachine)
        {
            EditorGUILayout.Space(5);
            EditorGUILayout.LabelField("Transitions", m_SectionHeaderStyle);
            
            // 转换规则数量
            var getAllTransitionsMethod = typeof(StateMachine).GetMethod("GetAllTransitions",
                BindingFlags.Public | BindingFlags.Instance);

            if (getAllTransitionsMethod != null)
            {
                if (getAllTransitionsMethod.Invoke(stateMachine, null) is
                    IReadOnlyDictionary<Type, List<Type>> transitionsDict)
                {
                    int transitionCount = 0;
                    foreach (var kvp in transitionsDict)
                    {
                        if (kvp.Value != null)
                        {
                            transitionCount += kvp.Value.Count;
                        }
                    }

                    EditorGUILayout.LabelField($"Count: {transitionCount}", m_BoldLabelStyle);
                    EditorGUILayout.Space(2);

                    if (transitionCount > 0)
                    {
                        EditorGUI.indentLevel++;
                        foreach (var kvp in transitionsDict)
                        {
                            if (kvp.Value != null && kvp.Value.Count > 0)
                            {
                                EditorGUILayout.LabelField($"{kvp.Key.Name}:", m_SubHeaderStyle);
                            EditorGUILayout.Space(1);
                                EditorGUI.indentLevel++;
                                foreach (var targetState in kvp.Value)
                                {
                                    EditorGUILayout.LabelField($"→ {targetState.Name}", m_LabelStyle);
                                }

                                EditorGUI.indentLevel--;
                            }
                        }

                        EditorGUI.indentLevel--;
                    }
                }
            }
        }

        #endregion

        #region SimpleSceneEntity MVC

        private void DrawSimpleSceneEntityMVC()
        {
            var entity = target as Entity;
            if (entity == null) return;

            GUILayout.BeginVertical(m_BoxStyle);
            
            showMVC = EditorGUILayout.Foldout(showMVC, 
                new GUIContent((showMVC ? FOLDOUT_OPEN_PREFIX : FOLDOUT_CLOSED_PREFIX) + "MVC Components"), 
                true, m_FoldoutStyle);
            
            if (showMVC)
            {
                EditorGUI.indentLevel++;
                DrawSimpleSceneEntityMVC(entity);
                EditorGUI.indentLevel--;
            }
            GUILayout.EndVertical();
            EditorGUILayout.Space();
        }

        private void DrawSimpleSceneEntityMVC(Entity entity)
        {
            Type mvcEntityType = FindMvcEntityType(entity.GetType());
            if (mvcEntityType != null)
            {
                DrawMVCInfo(entity);
            }
        }

        private bool IsMvcEntity(Entity entity)
        {
            Type entityType = entity.GetType();
            return FindMvcEntityType(entityType) != null;
        }

        private Type FindMvcEntityType(Type type)
        {
            // 遍历继承链，查找MvcEntity<>基类
            while (type != null && type != typeof(object))
            {
                // 检查当前类型是否继承自MvcEntity<>
                if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(MvcEntity<>))
                {
                    return type;
                }

                type = type.BaseType;
            }

            return null;
        }

        private void DrawMVCInfo(Entity entity)
        {
            // 获取entity的实际类型
            Type entityType = entity.GetType();

            // 查找实际的MvcEntity<T>类型
            Type actualMvcType = FindMvcEntityType(entityType);

            // 如果找不到MvcEntity基类，直接使用entityType
            if (actualMvcType == null)
            {
                // 获取泛型MvcEntity<>类型
                Type baseMvcType = typeof(MvcEntity<>);
                try
                {
                    Type genericMvcType = baseMvcType.MakeGenericType(entityType);
                    DrawModelsInfo(entity, genericMvcType);
                    DrawViewsInfo(entity, genericMvcType);
                    DrawControllersInfo(entity, genericMvcType);
                }
                catch (ArgumentException)
                {
                    EditorGUILayout.LabelField("无法创建泛型类型", EditorStyles.helpBox);
                }
            }
            else
            {
                // 使用找到的MvcEntity<T>类型来获取泛型参数
                Type[] genericArgs = actualMvcType.GetGenericArguments();
                if (genericArgs.Length > 0)
                {
                    Type baseMvcType = typeof(MvcEntity<>);
                    try
                    {
                        Type genericMvcType = baseMvcType.MakeGenericType(genericArgs[0]);
                        DrawModelsInfo(entity, genericMvcType);
                        DrawViewsInfo(entity, genericMvcType);
                        DrawControllersInfo(entity, genericMvcType);
                    }
                    catch (ArgumentException)
                    {
                        EditorGUILayout.LabelField("无法创建泛型类型", EditorStyles.helpBox);
                    }
                }
                else
                {
                    // fallback到原始逻辑
                    Type baseMvcType = typeof(MvcEntity<>);
                    try
                    {
                        Type genericMvcType = baseMvcType.MakeGenericType(entityType);
                        DrawModelsInfo(entity, genericMvcType);
                        DrawViewsInfo(entity, genericMvcType);
                        DrawControllersInfo(entity, genericMvcType);
                    }
                    catch (ArgumentException)
                    {
                        EditorGUILayout.LabelField("无法创建泛型类型", EditorStyles.helpBox);
                    }
                }
            }
        }

        private void DrawModelsInfo(Entity entity, Type genericMvcType)
        {
            EditorGUILayout.LabelField("Models", m_SectionHeaderStyle);
            
            // 显示Model信息
            var modelsField = genericMvcType.GetField("mModels",
                BindingFlags.NonPublic | BindingFlags.Instance);

            if (modelsField != null)
            {
                var models = modelsField.GetValue(entity) as System.Collections.IList;
                if (models != null)
                {
                    EditorGUILayout.LabelField($"Count: {models.Count}", m_BoldLabelStyle);
                    EditorGUILayout.Space(2);
                    if (models.Count > 0)
                    {
                        EditorGUI.indentLevel++;
                        for (int i = 0; i < models.Count; i++)
                        {
                            var model = models[i];
                            if (model != null)
                            {
                                EditorGUILayout.LabelField(model.GetType().Name, m_LabelStyle);
                            }
                        }

                        EditorGUI.indentLevel--;
                    }
                }
            }
        }

        private void DrawViewsInfo(Entity entity, Type genericMvcType)
        {
            EditorGUILayout.Space(5);
            EditorGUILayout.LabelField("Views", m_SectionHeaderStyle);
            
            // 显示View信息
            var viewsField = genericMvcType.GetField("mViews", BindingFlags.NonPublic | BindingFlags.Instance);
            if (viewsField != null)
            {
                var views = viewsField.GetValue(entity) as System.Collections.IList;
                if (views != null)
                {
                    EditorGUILayout.LabelField($"Count: {views.Count}", m_BoldLabelStyle);
                    EditorGUILayout.Space(2);
                    if (views.Count > 0)
                    {
                        EditorGUI.indentLevel++;
                        for (int i = 0; i < views.Count; i++)
                        {
                            var view = views[i];
                            if (view != null)
                            {
                                // 检查是否是Unity组件
                                if (view is Component viewComponent)
                                {
                                    EditorGUILayout.ObjectField(view.GetType().Name, viewComponent, typeof(Component), false);
                                }
                                else
                                {
                                    EditorGUILayout.LabelField(view.GetType().Name, m_LabelStyle);
                                }
                            }
                        }

                        EditorGUI.indentLevel--;
                    }
                }
            }
        }

        private void DrawControllersInfo(Entity entity, Type genericMvcType)
        {
            EditorGUILayout.Space(5);
            EditorGUILayout.LabelField("Controllers", m_SectionHeaderStyle);
            
            // 显示Controller信息
            var controllersField =
                genericMvcType.GetField("mControllers", BindingFlags.NonPublic | BindingFlags.Instance);
            if (controllersField != null)
            {
                var controllers = controllersField.GetValue(entity) as System.Collections.IList;
                if (controllers != null)
                {
                    EditorGUILayout.LabelField($"Count: {controllers.Count}", m_BoldLabelStyle);
                    EditorGUILayout.Space(2);
                    if (controllers.Count > 0)
                    {
                        EditorGUI.indentLevel++;
                        for (int i = 0; i < controllers.Count; i++)
                        {
                            var controller = controllers[i];
                            if (controller != null)
                            {
                                EditorGUILayout.LabelField(controller.GetType().Name, m_LabelStyle);
                            }
                        }

                        EditorGUI.indentLevel--;
                    }
                }
            }
        }

        #endregion

        #region Entity Components

        private void DrawEntityComponents(Entity entity)
        {
            GUILayout.BeginVertical(m_BoxStyle);
            
            showEntityComponents = EditorGUILayout.Foldout(showEntityComponents, 
                new GUIContent((showEntityComponents ? FOLDOUT_OPEN_PREFIX : FOLDOUT_CLOSED_PREFIX) + "Entity Components"), 
                true, m_FoldoutStyle);
            
            if (showEntityComponents)
            {
                EditorGUI.indentLevel++;
                
                // 检查 Entity 是否已初始化，未初始化时无法获取组件
                if (entity.IsInit)
                {
                    var components = entity.GetAllEntityComponents();
                    EditorGUILayout.LabelField($"Count: {components.Count}", m_BoldLabelStyle);
                    
                    if (components.Count > 0)
                    {
                        EditorGUI.indentLevel++;
                        foreach (var component in components)
                        {
                            if (component != null)
                            {
                                // 检查是否是Unity组件
                                if (component is Component unityComponent)
                                {
                                    EditorGUILayout.ObjectField(component.GetType().Name, unityComponent, typeof(Component), false);
                                }
                                else
                                {
                                    EditorGUILayout.LabelField(component.GetType().Name, m_LabelStyle);
                                }
                            }
                        }
                        EditorGUI.indentLevel--;
                    }
                    else
                    {
                        EditorGUILayout.LabelField("No components found", m_LabelStyle);
                    }
                }
                else
                {
                    EditorGUILayout.LabelField("Entity not initialized", m_LabelStyle);
                }
                
                EditorGUI.indentLevel--;
            }
            GUILayout.EndVertical();
            EditorGUILayout.Space();
        }

        #endregion

        #region Public Fields

        private void DrawPublicFields()
        {
            var entity = target as Entity;
            if (entity == null) return;

            GUILayout.BeginVertical(m_BoxStyle);
            
            showPublicFields = EditorGUILayout.Foldout(showPublicFields, 
                new GUIContent((showPublicFields ? FOLDOUT_OPEN_PREFIX : FOLDOUT_CLOSED_PREFIX) + "Public Fields"), 
                true, m_FoldoutStyle);
            
            if (showPublicFields)
            {
                EditorGUI.indentLevel++;
                DrawPublicFieldsList(entity);
                EditorGUI.indentLevel--;
            }
            GUILayout.EndVertical();
            EditorGUILayout.Space();
        }

        private void DrawPublicFieldsList(Entity entity)
        {
            Type entityType = entity.GetType();
            // 获取所有公共实例字段，包括继承的字段，但排除只读和常量字段
            FieldInfo[] publicFields = entityType.GetFields(BindingFlags.Public | BindingFlags.Instance);
            
            // 获取所有标记为 SerializeField 的私有字段
            FieldInfo[] serializedPrivateFields = entityType.GetFields(BindingFlags.NonPublic | BindingFlags.Instance)
                .Where(f => f.GetCustomAttribute<SerializeField>() != null)
                .ToArray();

            // 合并公共字段和序列化的私有字段
            FieldInfo[] allFields = publicFields.Concat(serializedPrivateFields).ToArray();

            if (allFields.Length > 0)
            {
                bool hasDisplayableFields = false;
                
                foreach (FieldInfo field in allFields)
                {
                    // 跳过const和readonly字段
                    if (field.IsLiteral || field.IsInitOnly) 
                        continue;
                    
                    // 检查是否有HideInInspector属性
                    var hideAttributes = field.GetCustomAttributes(typeof(HideInInspector), true);
                    if (hideAttributes != null && hideAttributes.Length > 0)
                        continue;

                    hasDisplayableFields = true;
                    object value = field.GetValue(entity);
                    Type fieldType = field.FieldType;

                    if (fieldType == typeof(int))
                    {
                        int newValue = EditorGUILayout.IntField(ObjectNames.NicifyVariableName(field.Name), (int)value);
                        if (!Equals(newValue, value)) {
                            Undo.RecordObject(entity, $"Modify {field.Name}");
                            field.SetValue(entity, newValue);
                            EditorUtility.SetDirty(entity);
                        }
                    }
                    else if (fieldType == typeof(float))
                    {
                        float newValue = EditorGUILayout.FloatField(ObjectNames.NicifyVariableName(field.Name), (float)value);
                        if (!Equals(newValue, value)) {
                            Undo.RecordObject(entity, $"Modify {field.Name}");
                            field.SetValue(entity, newValue);
                            EditorUtility.SetDirty(entity);
                        }
                    }
                    else if (fieldType == typeof(bool))
                    {
                        bool newValue = EditorGUILayout.Toggle(ObjectNames.NicifyVariableName(field.Name), (bool)value);
                        if (!Equals(newValue, value)) {
                            Undo.RecordObject(entity, $"Modify {field.Name}");
                            field.SetValue(entity, newValue);
                            EditorUtility.SetDirty(entity);
                        }
                    }
                    else if (fieldType == typeof(string))
                    {
                        string newValue = EditorGUILayout.TextField(ObjectNames.NicifyVariableName(field.Name), (string)value);
                        if (!Equals(newValue, value)) {
                            Undo.RecordObject(entity, $"Modify {field.Name}");
                            field.SetValue(entity, newValue);
                            EditorUtility.SetDirty(entity);
                        }
                    }
                    else if (fieldType == typeof(Vector2))
                    {
                        Vector2 newValue = EditorGUILayout.Vector2Field(ObjectNames.NicifyVariableName(field.Name), (Vector2)value);
                        if (!Equals(newValue, value)) {
                            Undo.RecordObject(entity, $"Modify {field.Name}");
                            field.SetValue(entity, newValue);
                            EditorUtility.SetDirty(entity);
                        }
                    }
                    else if (fieldType == typeof(Vector3))
                    {
                        Vector3 newValue = EditorGUILayout.Vector3Field(ObjectNames.NicifyVariableName(field.Name), (Vector3)value);
                        if (!Equals(newValue, value)) {
                            Undo.RecordObject(entity, $"Modify {field.Name}");
                            field.SetValue(entity, newValue);
                            EditorUtility.SetDirty(entity);
                        }
                    }
                    else if (fieldType == typeof(Color))
                    {
                        Color newValue = EditorGUILayout.ColorField(ObjectNames.NicifyVariableName(field.Name), (Color)value);
                        if (!Equals(newValue, value)) {
                            Undo.RecordObject(entity, $"Modify {field.Name}");
                            field.SetValue(entity, newValue);
                            EditorUtility.SetDirty(entity);
                        }
                    }
                    else if (fieldType == typeof(Bounds))
                    {
                        Bounds newValue = EditorGUILayout.BoundsField(ObjectNames.NicifyVariableName(field.Name), (Bounds)value);
                        if (!Equals(newValue, value)) {
                            Undo.RecordObject(entity, $"Modify {field.Name}");
                            field.SetValue(entity, newValue);
                            EditorUtility.SetDirty(entity);
                        }
                    }
                    else if (fieldType == typeof(Rect))
                    {
                        Rect newValue = EditorGUILayout.RectField(ObjectNames.NicifyVariableName(field.Name), (Rect)value);
                        if (!Equals(newValue, value)) {
                            Undo.RecordObject(entity, $"Modify {field.Name}");
                            field.SetValue(entity, newValue);
                            EditorUtility.SetDirty(entity);
                        }
                    }
                    else if (fieldType.IsEnum)
                    {
                        Enum newValue = EditorGUILayout.EnumPopup(ObjectNames.NicifyVariableName(field.Name), (Enum)value, m_EnumStyle);
                        if (!Equals(newValue, value)) {
                            Undo.RecordObject(entity, $"Modify {field.Name}");
                            field.SetValue(entity, newValue);
                            EditorUtility.SetDirty(entity);
                        }
                    }
                    else if (typeof(Object).IsAssignableFrom(fieldType))
                    {
                        Object newValue = EditorGUILayout.ObjectField(ObjectNames.NicifyVariableName(field.Name), (Object)value, fieldType, true);
                        if (newValue != (Object)value) {
                            Undo.RecordObject(entity, $"Modify {field.Name}");
                            field.SetValue(entity, newValue);
                            EditorUtility.SetDirty(entity);
                        }
                    }
                    else
                    {
                        EditorGUILayout.LabelField(ObjectNames.NicifyVariableName(field.Name), value?.ToString() ?? "Null", m_ValueLabelStyle);
                    }
                }
                
                if (!hasDisplayableFields)
                {
                    EditorGUILayout.LabelField("No public fields", m_LabelStyle);
                    EditorGUILayout.Space(2);
                    EditorGUILayout.Space(2);
                }
            }
            else
            {
                EditorGUILayout.LabelField("No public fields", m_LabelStyle);
            }
        }

        #endregion
        
        #region Default Inspector


        #endregion
    }
}